package com.yamaikeji.plan.controller;

import com.yamaikeji.common.annotation.Log;
import com.yamaikeji.common.core.controller.BaseController;
import com.yamaikeji.common.core.domain.AjaxResult;
import com.yamaikeji.common.enums.BusinessType;
import com.yamaikeji.plan.aop.CheckLogin;
import com.yamaikeji.system.domain.plan.Plan;
import com.yamaikeji.system.domain.plan.PlanType;
import com.yamaikeji.system.service.plan.IPlanService;
import com.yamaikeji.system.service.plan.IPlanTypeService;
import com.yamaikeji.system.util.IdCreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 计划类型Controller
 * 
 * @author yamaikeji
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/api/plan/planType")
public class PlanTypeController extends BaseController {

    private static final String DEFAULT_PLAN_TYPE_NO = "PT100000000000000000";

    @Autowired
    private IPlanTypeService planTypeService;

    @Autowired
    private IPlanService planService;

    /**
     * 查询计划类型列表
     */
    @CheckLogin
    @PostMapping("/getPlanTypeList")
    public AjaxResult getPlanTypeList(HttpServletRequest req, @RequestBody PlanType planType) {
        String userNo = (String) req.getAttribute("userNo");
        planType.setUserNo(userNo);
        startPage();
        List<PlanType> list = planTypeService.selectPlanTypeList(planType);
        return AjaxResult.success(list);
    }

    /**
     * 查询计划类型详情
     */
    @CheckLogin
    @PostMapping("/getPlanType")
    public AjaxResult getPlanType(HttpServletRequest req, @RequestBody PlanType planType) {
        String userNo = (String) req.getAttribute("userNo");
        planType.setUserNo(userNo);
        planType = planTypeService.selectPlanType(planType);
        return AjaxResult.success(planType);
    }

    /**
     * 新增保存计划类型
     */
    @CheckLogin
    @Log(title = "计划类型", businessType = BusinessType.INSERT)
    @PostMapping("/addPlanType")
    public AjaxResult addPlanType(HttpServletRequest req, @RequestBody PlanType planType) {
        planType.setPlanTypeNo(IdCreateUtil.generateNo("PT"));
        String userNo = (String) req.getAttribute("userNo");
        planType.setUserNo(userNo);
        return AjaxResult.success(planTypeService.insertPlanType(planType));
    }

    /**
     * 修改保存计划类型
     */
    @CheckLogin
    @Log(title = "计划类型", businessType = BusinessType.UPDATE)
    @PostMapping("/editPlanType")
    public AjaxResult editPlanType(HttpServletRequest req, @RequestBody PlanType planType) {
        String userNo = (String) req.getAttribute("userNo");
        planType.setUserNo(userNo);
        return AjaxResult.success(planTypeService.updatePlanType(planType));
    }

    /**
     * 删除计划类型
     */
    @CheckLogin
    @Log(title = "计划类型", businessType = BusinessType.DELETE)
    @PostMapping( "/deletePlanType")
    public AjaxResult deletePlanType(HttpServletRequest req, @RequestBody PlanType planType) {
        String userNo = (String) req.getAttribute("userNo");
        planType.setUserNo(userNo);
        planTypeService.deletePlanType(planType);
        Plan plan = new Plan();
        plan.setUserNo(userNo);
        plan.setDefaultPlanTypeNo(DEFAULT_PLAN_TYPE_NO);
        plan.setPlanTypeNo(planType.getPlanTypeNo());
        planService.updatePlanType(plan);
        return AjaxResult.success(1);
    }
}
